package com.chen.excel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class WaybillImageService {

    @Autowired
    private ExcelReader excelReader;

    @Autowired
    private ImageQueryService imageQueryService;

    public List<ImageQueryService.WaybillImageResult> processExcelFile(String excelFilePath) throws IOException {
        List<WaybillData> waybillDataList = excelReader.readWaybillData(excelFilePath);
        return imageQueryService.processWaybillDataList(waybillDataList);
    }

    public ProcessResult processExcelFileWithSummary(String excelFilePath) throws IOException {
        List<WaybillData> waybillDataList = excelReader.readWaybillData(excelFilePath);
        List<ImageQueryService.WaybillImageResult> results = imageQueryService.processWaybillDataList(waybillDataList);
        
        ProcessResult processResult = new ProcessResult();
        processResult.setResults(results);
        processResult.setTotalRecords(waybillDataList.size());
        
        int foundRecords = 0;
        int totalImages = 0;
        
        for (ImageQueryService.WaybillImageResult result : results) {
            if (!result.getImageUrls().isEmpty()) {
                foundRecords++;
                totalImages += result.getImageUrls().size();
            }
        }
        
        processResult.setFoundRecords(foundRecords);
        processResult.setTotalImages(totalImages);
        processResult.setNotFoundRecords(waybillDataList.size() - foundRecords);
        
        return processResult;
    }

    public List<ImageQueryService.WaybillImageResult> processWaybillDataList(List<WaybillData> waybillDataList) {
        return imageQueryService.processWaybillDataList(waybillDataList);
    }

    public static class ProcessResult {
        private List<ImageQueryService.WaybillImageResult> results;
        private int totalRecords;
        private int foundRecords;
        private int notFoundRecords;
        private int totalImages;

        public List<ImageQueryService.WaybillImageResult> getResults() {
            return results;
        }

        public void setResults(List<ImageQueryService.WaybillImageResult> results) {
            this.results = results;
        }

        public int getTotalRecords() {
            return totalRecords;
        }

        public void setTotalRecords(int totalRecords) {
            this.totalRecords = totalRecords;
        }

        public int getFoundRecords() {
            return foundRecords;
        }

        public void setFoundRecords(int foundRecords) {
            this.foundRecords = foundRecords;
        }

        public int getNotFoundRecords() {
            return notFoundRecords;
        }

        public void setNotFoundRecords(int notFoundRecords) {
            this.notFoundRecords = notFoundRecords;
        }

        public int getTotalImages() {
            return totalImages;
        }

        public void setTotalImages(int totalImages) {
            this.totalImages = totalImages;
        }

        @Override
        public String toString() {
            return "ProcessResult{" +
                    "totalRecords=" + totalRecords +
                    ", foundRecords=" + foundRecords +
                    ", notFoundRecords=" + notFoundRecords +
                    ", totalImages=" + totalImages +
                    '}';
        }
    }
}